package cn.hsw.manage.center.service.impl;

import cn.hsw.manage.center.entity.UserRoleCenter;
import cn.hsw.manage.center.mapper.UserRoleCenterMapper;
import cn.hsw.manage.center.service.UserRoleCenterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李子木
 */
@Service
public class UserRoleCenterServiceImpl extends ServiceImpl<UserRoleCenterMapper,UserRoleCenter> implements UserRoleCenterService {


    @Override
    public void bind(Long userPid, String rolePids) {
        baseMapper.deleteByUserPid(userPid);
        //后台管理新增用户绑定角色
        String[] rolePid = rolePids.split(",");

        List<UserRoleCenter> insList = Arrays.stream(rolePid).map(s -> UserRoleCenter.builder()
                .userPid(userPid)
                .rolePid(Long.parseLong(s))
                .build()).collect(Collectors.toList());
        this.saveBatch(insList);
    }

    @Override
    public boolean validateRoleHaveUser(Long pid) {
        return baseMapper.countByRolePid(pid)>0;
    }

    @Override
    public void deleteByUserPid(Long userPid) {
        baseMapper.deleteByUserPid(userPid);
    }
}
